"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toFixed = exports.randomStr = exports.getElementRect = exports.getCanvas = void 0;

var _taro = _interopRequireDefault(require("@tarojs/taro"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var toFixed = function toFixed() {
  var number = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  return 0.01 * Math.floor(100 * number);
}; // 随机数，获取当前canvas id


exports.toFixed = toFixed;

var randomStr = function randomStr() {
  var len = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 16;
  var string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var l = string.length;
  var str = '';

  for (var i = 0; i < len; i++) {
    var index = Math.floor(Math.random() * 100 * l % l);
    str += string[index];
  }

  return str;
};

exports.randomStr = randomStr;

var getElementRect = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var eleId,
        delay,
        _args = arguments;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            eleId = _args.length > 0 && _args[0] !== undefined ? _args[0] : '';
            delay = _args.length > 1 && _args[1] !== undefined ? _args[1] : 200;
            return _context.abrupt("return", new Promise(function (resovle, reject) {
              var t = setTimeout(function () {
                clearTimeout(t);

                _taro.default.createSelectorQuery().select("#".concat(eleId)).boundingClientRect(function (rect) {
                  if (rect) {
                    resovle(rect);
                  } else {
                    reject('获取不到元素');
                  }
                }).exec();
              }, delay);
            }));

          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));

  return function getElementRect() {
    return _ref.apply(this, arguments);
  };
}();

exports.getElementRect = getElementRect;

var getCanvas = /*#__PURE__*/function () {
  var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
    var eleId,
        delay,
        _args2 = arguments;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            eleId = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : '';
            delay = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : 200;
            return _context2.abrupt("return", new Promise(function (resolve, reject) {
              var t = setTimeout(function () {
                clearTimeout(t);

                _taro.default.createSelectorQuery().select("#".concat(eleId)).fields({
                  node: true,
                  size: true
                }).exec(function (res) {
                  if (res && res[0] && res[0].node) {
                    var canvas = res[0].node;
                    var ctx = canvas.getContext('2d');
                    resolve({
                      canvas: canvas,
                      ctx: ctx
                    });
                  } else {
                    reject('获取canvas失败');
                  }
                });
              }, delay);
            }));

          case 3:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));

  return function getCanvas() {
    return _ref2.apply(this, arguments);
  };
}();

exports.getCanvas = getCanvas;